package gr.unipi.mscd1;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class DeviceTableModel implements TableModel, SnmpValuesListener {

	private ArrayList<TableModelListener> mListeners = null;
	private ArrayList<DvInfo> arrayData = null;
	private DeviceInfo infoRetriever;
	
	private static final String[] columnNames = new String[] {
		"Index",
		"Description",
		"Type",
		"Status"
	};
	
	public DeviceTableModel(SimpleSnmpClient client) {
		mListeners = new ArrayList<TableModelListener>();
		arrayData = new ArrayList<DvInfo>();
		
		infoRetriever = new DeviceInfo(client);
		infoRetriever.addSnmpValueListener(this);
		infoRetriever.start();
	}
	
	public void addInterfaceInfo(DvInfo info) {
		arrayData.add(info);
		for (TableModelListener listener : mListeners) {
			TableModelEvent e = new TableModelEvent(this, arrayData.size() - 1, arrayData.size() - 1,
					TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
			
			listener.tableChanged(e);
		}
	}
	
	@Override
	public void addTableModelListener(TableModelListener listener) {
		mListeners.add(listener);
	}

	@Override
	public Class<?> getColumnClass(int columnId) {
		Object val = getValueAt(0, columnId);
		if (val == null) {
			System.out.println("Null value at columnid: " + columnId);
		}
		return val.getClass();
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public String getColumnName(int columnId) {
		return columnNames[columnId];
	}

	@Override
	public int getRowCount() {
		return arrayData.size();
	}

	@Override
	public Object getValueAt(int rowId, int columnId) {
		if ((rowId >= 0) && (rowId < arrayData.size())) {
			switch (columnId) {
			case 0:         //Index
				return arrayData.get(rowId).getId();
			case 1:         //Description
				return arrayData.get(rowId).getDescr();
			case 2:         //Type
				return arrayData.get(rowId).getTypeDescr();
			case 3:         //Status
				return arrayData.get(rowId).getStateDescr();
			}
		}
		return "";
	}

	@Override
	public boolean isCellEditable(int rowId, int columnId) {
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener listener) {
		mListeners.remove(listener);
	}

	@Override
	public void setValueAt(Object value, int rowId, int columnId) { }
	
	public int getIndexAtRow(int rowId) {
		if ((rowId >= 0) && (rowId < arrayData.size())) {
			return arrayData.get(rowId).getId();
		}
		return -1;
	}
	
	public void clearData() {
		if (arrayData.size() > 0) {
			int rowCount = arrayData.size();
			arrayData.clear();
			
			for (TableModelListener listener : mListeners) {
				TableModelEvent e = new TableModelEvent(this, 0, rowCount - 1,
						TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
				
				listener.tableChanged(e);
			}
		}
	}

	@Override
	public void valuesRetrieved(Object sender) {
		if (sender instanceof DeviceInfo) {
			DeviceInfo getter = (DeviceInfo)sender;
			
			DvInfo newInfo = new DvInfo(getter);
			addInterfaceInfo(newInfo);
		}
	}
}
